package org.shaitu.wakeremote4j;

import org.shaitu.wakeremote4j.util.StringUtil;

public class HostBean {
	private String name;
	private String mac;
	private String ip;
	private int port;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMac() {
		return mac;
	}
	public void setMac(String mac) {
		this.mac = mac;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	
	/**
	 * get hex byte of mac address
	 * @return
	 */
	public byte[] getHexMac(){
		return StringUtil.strToHex(mac.trim().replaceAll("[:-]",""));
	}
	
	/**
	 * construct bean by parsing bean string
	 * @param beanStr
	 * @return
	 */
	public static HostBean parseBean(String beanStr){
		if(StringUtil.isNullOrBlank(beanStr)){
			return null;
		}
		String[] beanValues = beanStr.split("\t");
		HostBean bean = new HostBean();
		bean.setName(beanValues[0]);
		bean.setMac(beanValues[1]);
        if(beanValues.length > 2){
            bean.setIp(beanValues[2]);
        }
        if(beanValues.length > 3){
            bean.setPort(Integer.parseInt(beanValues[3]));
        }
		return bean;
	}
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append(name).append("\t");
		sb.append(mac).append("\t");
		sb.append((ip!=null)?ip:"").append("\t");
		sb.append(port).append("\t");
		return sb.toString();
	}

}
